package com.jiuyou.storeController.technician;

import com.jiuyou.storeController.common.StoreBaseController;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.StoreUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.CopyUtils;
import com.ruoyi.common.core.domain.entity.StoreUserRole;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.store.service.IStoreUserRoleService;
import com.ruoyi.storeUser.service.IStoreUserService;
import com.ruoyi.technician.domain.StoreTechnician;
import com.ruoyi.technician.domain.StoreTechnicianVo;
import com.ruoyi.technician.service.IStoreTechnicianService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chengliang
 * @date 2021/11/24
 **/
@RestController
@RequestMapping("/store/technician")
@Api(tags = {"技师信息控制器"})
public class TechnicianStoreController extends StoreBaseController {

    @Autowired
    private IStoreTechnicianService technicianService;
    @Autowired
    private IStoreUserRoleService roleService;
    @Autowired
    private IStoreUserService storeUserService;
    @PutMapping
    @ApiOperation("添加技师")
    public StoreTechnician create(@ApiParam @RequestBody StoreTechnician technician){
        StoreUserRole role = getStoreUserRole();
        if(role == null){
            throw new CustomException("权限不足");
        }
        Long storeId= role.getStoreId();
        String phone=technician.getTechPhone();
        Long storeUserid=null;
        if(!StringUtils.isEmpty(phone)){
            StoreUser storeUsers=new StoreUser();
            storeUsers.setPhone(phone);
            List<StoreUser> list=storeUserService.selectStoreUserList(storeUsers);
            if(list.size()>0){
                storeUserid=  list.get(0).getId();
            }
        }else{
            throw new CustomException("手机号必填");
        }
        //新增时校验这个手机号是否已经存在，存在的话不让添加
        StoreUserRole storeUserRoleS=new StoreUserRole();
        storeUserRoleS.setStoreId(storeId);
        storeUserRoleS.setRoleType(DicConstants.STORE_Identity_JiShi.getCode());
        storeUserRoleS.setPhone(phone);
        List listC=  roleService.selectStoreUserRoleList(storeUserRoleS);
        if(listC.size()>0){
            throw new CustomException("手机号"+phone+"已存在");
        }
        Long userRoleId=  addStoreUserRoleJishi(storeId,phone,storeUserid,null);
        technician.setStoreId(storeId);
        technician.setState(DicConstants.SYS_Y.getCode());
        technician.setCreateBy(getLoginStoreUserId());
        technician.setCreateType(DicConstants.BUSINESS_Business.getCode());
        technician.setUserRoleId(userRoleId);
//        technician.setStoreUserId(storeUserid);
        technicianService.insertStoreTechnician(technician);
        return technician;
    }

    private Long addStoreUserRoleJishi(Long storeId,String phone,Long  storeUserid,Long userRoleIdOld){
            //新增生成新的uuid
//        String uuid= IdUtils.fastUUID();
        //添加技师的时候增加技师权限
        StoreUserRole storeUserRole=new StoreUserRole();
        storeUserRole.setStoreId(storeId);
        if(null!=phone){
            storeUserRole.setPhone(phone);
        }
        storeUserRole.setStoreUserId(storeUserid);
        if(null==userRoleIdOld){
            //新增
            storeUserRole.setRoleType(DicConstants.STORE_Identity_JiShi.getCode());
            roleService.insertStoreUserRole(storeUserRole);
        }else{
            //修改
            storeUserRole.setId(userRoleIdOld);
            roleService.updateStoreUserRole(storeUserRole);
        }

        return storeUserRole.getId();
    }
    @PostMapping
    @ApiOperation("修改技师")
    public StoreTechnician edit(@ApiParam @RequestBody StoreTechnician technician){
        StoreUserRole role = getStoreUserRole();
        if(role == null){
            throw new CustomException("权限不足");
        }
        Long storeId= role.getStoreId();
        String phone=technician.getTechPhone();
        Long storeUserid=null;
        if(!StringUtils.isEmpty(phone)){
            StoreUser storeUsers=new StoreUser();
            storeUsers.setPhone(phone);
            List<StoreUser> list=storeUserService.selectStoreUserList(storeUsers);
            if(list.size()>0){
                storeUserid=  list.get(0).getId();
            }
        }else{
            throw new CustomException("手机号必填");
        }
        //同门店下非uuid有phone，说明手机号冲突
        StoreUserRole storeUserRoleS=new StoreUserRole();
        storeUserRoleS.setStoreId(storeId);
        storeUserRoleS.setPhone(phone);
        storeUserRoleS.setRoleType(DicConstants.STORE_Identity_JiShi.getCode());
        storeUserRoleS.getParams().put("idueq",technician.getUserRoleId());
        List listC=  roleService.selectStoreUserRoleList(storeUserRoleS);
        if(listC.size()>0){
            throw new CustomException("手机号"+phone+"已存在");
        }
        Long userRoleIdOld=technician.getUserRoleId();
        Long userRoleId=  addStoreUserRoleJishi(storeId,phone,storeUserid,userRoleIdOld);
        technician.setUserRoleId(userRoleId);
//        technician.setStoreUserId(storeUserid);
        technician.setState(DicConstants.SYS_N.getCode());
        technician.setUpdateBy(getLoginStoreUserId());
        technician.setUpdateType(DicConstants.BUSINESS_Business.getCode());
        technicianService.updateStoreTechnician(technician);
        return technician;
    }
    @PostMapping("/open/{technicianId}")
    @ApiOperation("技师上班")
    public boolean open (@PathVariable Long technicianId){
        StoreUserRole role = getStoreUserRole();
        if(role == null){
            throw new CustomException("权限不足");
        }

        StoreTechnician t = technicianService.selectStoreTechnicianById(technicianId);
        Long storeId= role.getStoreId();
        if(storeId!=t.getStoreId()){
            throw new CustomException("权限不足");
        }

        String phone=t.getTechPhone();
        Long storeUserid=null;
        if(!StringUtils.isEmpty(phone)){
            StoreUser storeUsers=new StoreUser();
            storeUsers.setPhone(phone);
            List<StoreUser> list=storeUserService.selectStoreUserList(storeUsers);
            if(list.size()>0){
                storeUserid=  list.get(0).getId();
            }
        }else{
            throw new CustomException("手机号必填");
        }
        Long userRoleId=  addStoreUserRoleJishi(storeId,t.getTechPhone(),storeUserid,null);
        t.setUpdateType(DicConstants.BUSINESS_Business.getCode());
        t.setUpdateBy(role.getStoreUserId());
        t.setState(DicConstants.SYS_Y.getCode());
        t.setUserRoleId(userRoleId);
        technicianService.updateStoreTechnician(t);

        return true;
    }
    @PostMapping("/close/{technicianId}")
    @ApiOperation("技师不上班")
    public boolean close (@PathVariable Long technicianId){
        StoreUserRole role = getStoreUserRole();
        if(role == null){
            throw new CustomException("权限不足");
        }
        StoreTechnician t = technicianService.selectStoreTechnicianById(technicianId);
        Long storeId= role.getStoreId();
        if(storeId!=t.getStoreId()){
            throw new CustomException("权限不足");
        }
        Long userRoleId=t.getUserRoleId();
        if(null!=userRoleId){
            Long[] roles = new Long[1];
            roles[0] = userRoleId;
            roleService.logicDeleteStoreUserRole(roles,DicConstants.BUSINESS_Business.getCode(),role.getStoreUserId());

        }


        t.setUpdateType(DicConstants.BUSINESS_Business.getCode());
        t.setUpdateBy(role.getStoreUserId());
        t.setState(DicConstants.SYS_N.getCode());
        technicianService.updateStoreTechnician(t);
        return true;
    }
    @PostMapping("/del/{technicianId}")
    @ApiOperation("删除技师")
    public void del (@PathVariable Long technicianId){
        StoreUserRole role = getStoreUserRole();
        if(role == null){
            throw new CustomException("权限不足");
        }
        Long storeId= role.getStoreId();
        Long[] longs = new Long[1];
        longs[0] = technicianId;
        StoreTechnician storeTechnician= technicianService.selectStoreTechnicianById(technicianId);
        Long userRoleId=storeTechnician.getUserRoleId();
        Long[] roles = new Long[1];
        roles[0] = userRoleId;
        if(storeId!=storeTechnician.getStoreId()){
            throw new CustomException("权限不足");
        }
        roleService.logicDeleteStoreUserRole(roles,DicConstants.BUSINESS_Business.getCode(),role.getStoreUserId());
        technicianService.logicDeleteStoreTechnician(longs,DicConstants.BUSINESS_Business.getCode(),role.getStoreUserId());
    }
    @PostMapping("/getOne/{technicianId}")
    @ApiOperation("获取技师")
    public StoreTechnician getOne (@PathVariable Long technicianId){
        StoreUserRole role = getStoreUserRole();
        if(role == null){
            throw new CustomException("权限不足");
        }
        StoreTechnician t = technicianService.selectStoreTechnicianById(technicianId);
        return t;
    }
    @PostMapping("/getOneInfo")
    @ApiOperation("获取技师")
    public StoreTechnician getOneInfo (){
       /* StoreUserRole role = getStoreUserRole(DicConstants.STORE_Identity_JiShi.getCode());
        if(role == null){
            throw new CustomException("权限不足");
        }
        List<String> list=getStoreUserRoleTypes();
       if( list.contains("store_jiShi")){
         Long storeUserRoleId=  role.getId();
           StoreTechnician storeTechnician=new StoreTechnician();
           storeTechnician.setUserRoleId(storeUserRoleId);
           List<StoreTechnician> storeTechnicianList=   technicianService.selectStoreTechnicianList(storeTechnician);
           if(storeTechnicianList.size()>0){
               return storeTechnicianList.get(0);
           }
       }else{
           throw new CustomException("权限不足");
       }*/
       return getStoreTechnician();
    }


    /**
     * 查询技师信息列表
     */
    @ApiOperation(value = "查询技师信息列表，涉及多分类，建议不分页")
    @GetMapping
    public AjaxResult list()
    {
        StoreUserRole storeUserRole = getStoreUserRole();
        StoreTechnician storeTechnicianParam=new StoreTechnician();
        storeTechnicianParam.setStoreId(storeUserRole.getStoreId());
        startPage();
        List<StoreTechnician> list = technicianService.selectStoreTechnicianList(storeTechnicianParam);

        List<StoreTechnicianVo> result = CopyUtils.copyList(list, StoreTechnicianVo.class);

//        result.forEach(e->{
//            e.setTechTypeName(DictUtils.getDictLabel("tech_type",e.getTechType()));
//        });
         TableDataInfo tableDataInfo =getDataTable(result);
        return AjaxResult.success(tableDataInfo);
    }

}
